package cn.sciento.farm.devicemanager.service.impl;

import cn.sciento.farm.devicemanager.dao.entity.DevTimer;
import cn.sciento.farm.devicemanager.dao.mapper.TimerMapper;
import cn.sciento.farm.devicemanager.quartz.service.QuartzTimerService;
import cn.sciento.farm.devicemanager.service.TimerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
public class TimerServiceImpl implements TimerService {

    @Autowired
    private TimerMapper timerMapper;

    @Autowired
    QuartzTimerService quartzTimerService;

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public int insert(DevTimer devTimer) {
        String loop = devTimer.getCirculation();
        if (StringUtils.isEmpty(loop)) {
            return 0;
        }
        int count = timerMapper.insert(devTimer);
        if(count >0){
            try {
                int en = quartzTimerService.insert(devTimer);
                if(en == 0){
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                }
            } catch (SchedulerException e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }
        return count;
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public int updateById(DevTimer devTimer) {
        String loop = devTimer.getCirculation();
        int time = devTimer.getStartTime();
        if (StringUtils.isEmpty(loop)) {
            return 0;
        }
        int count = timerMapper.updateById(devTimer);
        try {
            devTimer = timerMapper.selectById(devTimer.getId());
            int en = quartzTimerService.update(devTimer);
            if(en == 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        } catch (SchedulerException e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

        }
        return count;
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public int deleteById(long id) {
        int count = timerMapper.deleteById(id);
        if(count >0){
            try {
                quartzTimerService.deleteById(id);
            } catch (SchedulerException e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }
        return count;
    }

    @Override
    public List<DevTimer> getByControllerId(long controllerId) {
        QueryWrapper queryWrapper = new QueryWrapper<DevTimer>();
        queryWrapper.eq("controller_id",controllerId);
        List<DevTimer> timers = timerMapper.selectList(queryWrapper);
        return timers;
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public int deleteByControllerId(long controllerId) {
        List<DevTimer > timerList = getByControllerId(controllerId);
        int count = 0;
        for(DevTimer timer : timerList){
            deleteById(timer.getId());
        }
        return count;
    }

    @Override
    public DevTimer getById(long id) {
        return timerMapper.selectById(id);
    }
}
